package com.learning.jquizenglish.screen.flashcard;

import java.util.ArrayList;
import java.util.Collections;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.learning.jquizenglish.R;
import com.learning.jquizenglish.activity.FlashCardActivity;
import com.learning.jquizenglish.common.adapter.BaseListAdapter;
import com.learning.jquizenglish.common.fragment.JQuizEnglishFragment;
import com.learning.jquizenglish.model.FlashCardCategoryModel;
import com.learning.jquizenglish.model.FlashCardItemModel;
import com.learning.jquizenglish.model.JQuizRequestModel;
import com.learning.jquizenglish.model.LoadMoreInfo;
import com.learning.jquizenglish.network.JQuizEnglishApi;
import com.learning.jquizenglish.network.JQuizEnglishApi.Level;
import com.learning.jquizenglish.widget.LoadMoreListView;
import com.learning.jquizenglish.widget.LoadMoreListView.OnLoadMoreListener;
import com.learning.jquizenglish.widget.ViewHolder;
import com.learning.jquizenglish.widget.smoothprogressbar.SmoothProgressBar;

// TODO: Auto-generated Javadoc
/**
 * The Class FlashCardCategoryScreen.
 */
public class FlashCardCategoryScreen extends JQuizEnglishFragment implements OnItemClickListener, OnLoadMoreListener {

	/** The sp category. */
	private Spinner spCategory;
	
	/** The smooth progress bar. */
	private SmoothProgressBar smoothProgressBar;
	
	/** The list view category. */
	private LoadMoreListView listViewCategory;
	
	/** The flash card level. */
	private Level flashCardLevel;
	
	/** The m adapter. */
	private FlashCardCategoryAdapter mAdapter;
	
	/** The m load more info. */
	private LoadMoreInfo mLoadMoreInfo;
	
	/* (non-Javadoc)
	 * @see com.learning.jquizenglish.common.fragment.BaseFragment#onCreateContentView(android.view.LayoutInflater, android.view.ViewGroup)
	 */
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		View v = inflater.inflate(R.layout.screen_flashcard_category, container, false);
		spCategory = (Spinner) v.findViewById(R.id.spCategory);
		smoothProgressBar = (SmoothProgressBar) v.findViewById(R.id.progressbar);
		listViewCategory = (LoadMoreListView) v.findViewById(R.id.flashcardlist);
		mAdapter = new FlashCardCategoryAdapter(getActivity(), new ArrayList<FlashCardItemModel>());
		listViewCategory.setAdapter(mAdapter);
		listViewCategory.setOnItemClickListener(this);
		mLoadMoreInfo = new LoadMoreInfo();
		listViewCategory.setOnLoadMoreListener(this);
		initSpinnerData();
		return v;
	}
	
	@Override
	public void onLoadMore() {
		if (!mLoadMoreInfo.isEndOfList) {
			requestFlashCardCategory();
		}
	}
	
	/**
	 * Inits the spinner data.
	 */
	private void initSpinnerData(){
		final ArrayList<String> arrFlashCardLevel = new ArrayList<String>();
		arrFlashCardLevel.add("1. Elementary");
		arrFlashCardLevel.add("2. Intermediate");
		arrFlashCardLevel.add("3. Advanced");
	    ArrayAdapter<String> spAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_item_category, arrFlashCardLevel);
	    spAdapter.setDropDownViewResource(R.layout.spinner_item_black);
	    spCategory.setAdapter(spAdapter);
	    spCategory.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int pos, long arg3) {
				switch (pos) {
				case 0:
					flashCardLevel = Level.ELEMENTARY;
					break;
				case 1:
					flashCardLevel = Level.INTERMEDIATE;
					break;
				case 2:
					flashCardLevel = Level.ADVANCED;
					break;
				default:
					flashCardLevel = Level.ELEMENTARY;
					break;
				}
				mAdapter.clearData();
				mLoadMoreInfo.reset();
				requestFlashCardCategory();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
	}
	
	/**
	 * Request flash card category.
	 */
	private void requestFlashCardCategory(){
		prepareForStartRequest();
		JQuizRequestModel requestModel = JQuizEnglishApi.getRequestFlashCardCategory(flashCardLevel, mLoadMoreInfo.page++, mLoadMoreInfo.per_page);
		executeGetTask(requestModel, false, FlashCardCategoryModel.class, new JQuizTaskListener() {
			
			@Override
			public void onTaskComplete(Object object) {
				if(object!=null){
					FlashCardCategoryModel flashCategoryModel = (FlashCardCategoryModel) object;
					flashCategoryModel.sets.removeAll(Collections.singleton(null));
					mAdapter.addAll(flashCategoryModel.sets);
					if (flashCategoryModel.sets.size()==0) {
						mLoadMoreInfo.isEndOfList = true;
					}
					listViewCategory.onLoadMoreComplete();
				}else{
					mLoadMoreInfo.isEndOfList = true;
					listViewCategory.onLoadMoreComplete();
				}
				endRequest();
			}
		});
	}
	
	/**
	 * Prepare for start request.
	 */
	private void prepareForStartRequest(){
		spCategory.setEnabled(false);
		smoothProgressBar.setVisibility(View.VISIBLE);
	}
	
	/**
	 * End request.
	 */
	private void endRequest(){
		spCategory.setEnabled(true);
		smoothProgressBar.setVisibility(View.INVISIBLE);
	}
	
	/* (non-Javadoc)
	 * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
	 */
	@Override
	public void onItemClick(AdapterView<?> arg0, View row, int position, long arg3) {
		FlashCardScreen flashCardScreen = new FlashCardScreen();
		Bundle bundle = new Bundle();
		bundle.putLong("id", mAdapter.getItem(position).id);
		flashCardScreen.setArguments(bundle);
		((FlashCardActivity)getActivity()).switchContent(flashCardScreen, true);
	}
	
	
	/**
	 * The Class FlashCardCategoryAdapter.
	 */
	class FlashCardCategoryAdapter extends BaseListAdapter<FlashCardItemModel>{

		/**
		 * Instantiates a new flash card category adapter.
		 *
		 * @param activity the activity
		 * @param datas the datas
		 */
		public FlashCardCategoryAdapter(Activity activity,ArrayList<FlashCardItemModel> datas) {
			super(activity, datas);
		}

		/* (non-Javadoc)
		 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
		 */
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(mActivity).inflate(R.layout.flashcard_category_item, null);
			}
			TextView tvTitle = ViewHolder.get(convertView, R.id.tvTitle);
			TextView tvNumberCards = ViewHolder.get(convertView, R.id.tvNumberCards);
			TextView tvLanguage = ViewHolder.get(convertView, R.id.tvLanguage);
			TextView tvDescription = ViewHolder.get(convertView, R.id.tvDescription);
			FlashCardItemModel model = getItem(position);
			if (model!=null) {
				tvTitle.setText(model.title);
				tvNumberCards.setText(""+model.term_count);
				tvLanguage.setText(model.lang_terms+"-"+model.lang_definitions);
				tvDescription.setText("Created By "+model.created_by);
			}
			return convertView;
		}
		
		public void clearData(){
			mListDatas.clear();
			notifyDataSetChanged();
		}
		
		public void addAll(ArrayList<FlashCardItemModel> datas){
			mListDatas.addAll(datas);
			notifyDataSetChanged();
		}
		
	}

}
